package study.job.spring.support.trace;

import com.google.common.base.Stopwatch;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Aspect
@Component
public class JobTraceAspect {

    @Around("@annotation(jobTrace)")
    public Object around(ProceedingJoinPoint joinPoint, JobTrace jobTrace) throws Throwable {
        //cost time
        Stopwatch stopwatch = Stopwatch.createStarted();

        //log trace
        String name = jobTrace.name();
        String traceId = String.valueOf(System.currentTimeMillis());
//        MdcUtils.put(name, traceId);
        //
        try {
//            Signature sign = joinPoint.getSignature();
//            TrackLogger.info(sign.getDeclaringTypeName());
//            TrackLogger.info(sign.toShortString());
//            TrackLogger.info(sign.toLongString());
//            String kind = joinPoint.getKind();
            //
            return joinPoint.proceed();
        } catch (Throwable ex) {
            log.error("", ex);
            throw ex;
        } finally {
            log.info("[{}] cost time [{}] ms", name, stopwatch.elapsed(TimeUnit.MILLISECONDS));
//            MdcUtils.clear();
        }
    }
}
